import { Control } from 'react-keeper'
import nattyStorage from 'natty-storage';
import DB from './../../app/db';
import StorageUtils from '../../app/storage'
import Utils from "../../utils/index";
import {getProprietorByOpenId} from "../../pages/home/toupiao/const/Const";


export default (callback, props)=> {
  if (__LOCAL__) {
    StorageUtils.createUser();
    callback();
  } else {

    let userInfo = StorageUtils.getUser();
    if (userInfo && userInfo.openId !== 'xxx') {
      DB.WechatAPI.userIsBinding({openId: userInfo.openId}).then(
        function (content) {
          DB.WechatAPI.getProprietorByOpenId({openId: userInfo.openId}).then(
            function (content) {
              if (content && content.id) {
                StorageUtils.updateUser({proprietorId: content.id});
                StorageUtils.setStorage('proprietor', content);
              } else {
                StorageUtils.updateUser({proprietorId: ''});
                StorageUtils.removeStorageItem('proprietor');
              }
              callback();
            },
            function (error) {
              StorageUtils.updateUser({proprietorId: ''});
              StorageUtils.removeStorageItem('proprietor');
              Utils.myAlert(error);
            }
          ).catch(function (reason) {
            Utils.myAlert(reason);
          });
        },
        function (error) {
          Control.replace('/xq/register', userInfo);
        }
      );
    } else {

      /*const code = Utils.GetQueryString(window.location.search, 'code');
      DB.WechatAPI.getUser({code: code}).then(
        function (content) {
          storage.set('user', content);
          if (content && content.openId) {
            DB.WechatAPI.userIsBinding({openId: content.openId}).then(
              function (content) {
                DB.WechatAPI.getProprietorByOpenId({openId: content.openId}).then(
                  function (content) {
                    if (content && content.id) {
                      StorageUtils.updateUser({proprietorId: content.id});
                      StorageUtils.setStorage('proprietor', content);
                    } else {
                      StorageUtils.updateUser({proprietorId: ''});
                      StorageUtils.removeStorageItem('proprietor');
                    }
                    callback();
                  },
                  function (error) {
                    StorageUtils.updateUser({proprietorId: ''});
                    StorageUtils.removeStorageItem('proprietor');
                    Utils.myAlert(error);
                  }
                ).catch(function (reason) {
                  Utils.myAlert(reason);
                });
              },
              function (error) {
                Control.replace('/xq/register', content)
              }
            ).catch(function (reason) {
              Utils.myAlert(reason);
            });
          } else {
            Control.replace('/xq/register', content)
          }
        },
        function (error) {
          alert("系统错误，请确保在微信中打开链接。");
        }
      ).catch(function (reason) {
        Utils.myAlert(reason);
      });*/

      new Promise(function (resolve, reject) {
        StorageUtils.getUserInfo(resolve, reject);
      }).then(function (r) {
        userInfo = StorageUtils.getUser();
        if (userInfo && userInfo.openId !== 'xxx') {
          DB.WechatAPI.userIsBinding({openId: userInfo.openId}).then(
            function (content) {
              DB.WechatAPI.getProprietorByOpenId({openId: userInfo.openId}).then(
                function (content) {
                  if (content && content.id) {
                    StorageUtils.updateUser({proprietorId: content.id});
                    StorageUtils.setStorage('proprietor', content);
                  } else {
                    StorageUtils.updateUser({proprietorId: ''});
                    StorageUtils.removeStorageItem('proprietor');
                  }
                  callback();
                },
                function (error) {
                  StorageUtils.updateUser({proprietorId: ''});
                  StorageUtils.removeStorageItem('proprietor');
                  Utils.myAlert(error);
                }
              ).catch(function (reason) {
                Utils.myAlert(reason);
              });
            },
            function (error) {
              Control.replace('/xq/register', userInfo)
            }
          ).catch(function (reason) {
            Utils.myAlert(reason);
          });
        } else {
          Control.replace('/xq/register', userInfo)
        }
      }).catch(function (reason) {
        Control.replace('/xq/register', userInfo)
      });
    }
  }
}
